$(function(){
 
	$("#searchGIS").click(function(){
		
		var name = $("#name").val();
		var id = $("#id").val() ;
		var url ;
		if ($("#gis").val()=="")
		{
			 url = "http://gis.wyspyjj.gov.cn/WebGisApplication/editpoint.aspx?id="+id+"&name="+ name +"&password=e10adc3949ba59abbe56e057f20f883e&type=1&keepThis=true&TB_iframe=true&height=530&width=800";

		}else{
		    url = "http://gis.wyspyjj.gov.cn/WebGisApplication/editpoint.aspx?id="+id +"&password=e10adc3949ba59abbe56e057f20f883e&type=1&keepThis=true&TB_iframe=true&height=530&width=800";
		}
	 
		tb_show("添加企业地图位置", url , "thickbox");
		returnid = "#gis";
	});

	$("#createEmployees").click(function(){
		tb_show("添加企业员工", "/person/create?keepThis=true&TB_iframe=true&height=530&width=600", "thickbox");
		 returnid = "employees";
	});

	$("#searchEmployees").click(function(){
		tb_show("查找企业员工", "/person/search?keepThis=true&TB_iframe=true&height=530&width=600", "thickbox");
		returnid = "employees";
	}); 

	$("#createApothecary").click(function(){
		tb_show("添加药剂师", "/person/create?keepThis=true&TB_iframe=true&height=530&width=600", "thickbox");
		 returnid = "apothecary";
	});

	$("#searchApothecary").click(function(){
		tb_show("查找药剂师", "/person/search?keepThis=true&TB_iframe=true&height=530&width=600", "thickbox");
		returnid = "apothecary";
	}); 
/// http://gis.wyspyjj.gov.cn/WebGisApplication/editpoint.aspx?id=1&name=西安市德福堂药业有限公司&type=1&password=e10adc3949ba59abbe56e057f20f883e

    var button = $('#upload');
		
	new AjaxUpload(button, {
		action: '/fileUploader/process', 
		name: 'file',
		responseType: 'json',
		data : {
			'upload' : "attachments", 
			'successAction' : "upload",
			'successController' : "multimedia",
			'errorAction' : "upload",
			'errorController' : "multimedia"
		},  
		onSubmit : function(file, ext){ 
				$.jGrowl('开始上传！'); 
			    this.disable(); 
		},
		onComplete: function(file, response){  
 			this.enable(); 
			if (response.success)
			{	// add file to the list
				$.jGrowl("上传成功！");
				$('#multimediafile').append("<div><img src='"+response.message.url+"' class='picture' value='"+response.message.id+"'><input type='hidden' name='multimedia' value='"+response.message.id+"'></div>");
		   
			}else{
				$.jGrowl(response.message);
			}
		}
	});


    var button = $('#uploadmovie');
		
	new AjaxUpload(button, {
		action: '/fileUploader/process', 
		name: 'file',
		responseType: 'json',
		data : {
			'upload' : "movie", 
			'successAction' : "upload",
			'successController' : "multimedia",
			'errorAction' : "upload",
			'errorController' : "multimedia"
		},  
		onSubmit : function(file, ext){ 
				$.jGrowl('开始上传！'); 
			    this.disable(); 
		},
		onComplete: function(file, response){ 
			 
 			this.enable();
		 
			if (response.success)
			{	 
				$.jGrowl("上传成功！");
				$('#moviefile').append("<div><img src='"+response.message.url+"' class='video' value='"+response.message.id+"'><input type='hidden' name='multimedia' value='"+response.message.id+"'></div>");
		   
			}else{
				$.jGrowl(response.message);
			}
		}
	});
	
	$(".video").live("hover", function(event){
		
		if (event.type =='mouseenter'){
			var html = "<div class='videoManager' style='border:0;position:absolute;float:left;'>"+
				"<a href='/multimedia/transcoding/"+$(this).attr("value")+"' target='_blank'>截图</a> | "+
				"<a href='/medicineCompany/deleteMultimedia/"+$("#id").val()+"?mid="+$(this).attr("value")+"'>删除</a></div>";
			$(this).after(html);
			
		}else{
		//alert("tes");
			var offset = $(this).offset();
 			if(event.pageX  < offset.left || event.pageX  > offset.left + $(this).width()
				|| event.pageY  < offset.top || event.pageX  > offset.top + $(this).height()){
				$(".videoManager").remove();
			}
		} 
	});

	$(".picture").live("hover", function(event){
		
		if (event.type =='mouseenter'){
			var html = "<div class='pictureManager' style='border:0;position:absolute;float:left;'>"+
				"<a href='/medicineCompany/deleteMultimedia/"+$("#id").val()+"?mid="+$(this).attr("value")+"'>删除</a></div>";
			$(this).after(html);
			
		}else{
		//alert("tes");
			var offset = $(this).offset();
 			if(event.pageX  < offset.left || event.pageX  > offset.left + $(this).width()
				|| event.pageY  < offset.top || event.pageX  > offset.top + $(this).height()){
				$(".pictureManager").remove();
			}
		} 
	});

	// 图片删除和视频编码与删除
	$("#test").click(function(){
	
//		var j = "{success:true, latitude:12312, longitude:123123}";    
//		crossDomain(j);

		alert(button.attr("target"));
	});
});

function saveGIS(success,   latitude, longitude){
 
 
	if (success)
	{ 
		$("#gis").val($("#id").val());
		$("#latitude").val(latitude);
		$("#longitude").val(longitude);
	} 
	window.parent.tb_remove();
}


function crossDomain(json){
	var obj = eval("(" + json + ")"); 
	saveGIS(obj.success , obj.latitude , obj.longitude);
}


function save(id, name){
	//$(returnid).empty();
	var opt = "<input name='"+returnid+"' type='hidden' value='"+id+ "'>" + name + "<br>"
	$("#"+returnid + "Space").prepend(opt);
}
 